import { initSearchConfig, TableSearchPanel, XxpageContainer, XxRangePicker, XxTable } from "@/ui";
import { DatePicker, Form, Input, Select, Button, Space, Row, Col, Table } from "antd";
import React, { useEffect, useRef, useState } from "react";
import searchFields from "./config/searchFields";
import getActoinPanel from "./config/action";
import getColumns from "./config/columns";
import CateForm from "./cateForm";
import { cateApi } from "@/api";
import { globalModal } from "@/data";
import { deleteConfirm, successAlert } from "@/common/methods";


const { RangePicker } = DatePicker
/**@description 商品分类页面 */
const CategoryListPages = () => {
    const cateFormRef = useRef(null);
    const [params, setParams] = useState({ ...initSearchConfig(searchFields) })
    const [state, setState] = useState({
        data: [{
            children: []
        }],
        totalCount: 0
    })
    const [loading, setLoading] = useState(false);

    //搜索事件
    const onClickSearch = (value) => {
        setParams({
            ...params,
            ...value,
            current: 1,
        });
    };


    /**@description 查找列表 */
    const getList = async () => {
        setLoading(true);
        const { data } = await cateApi.getCateList({ pageSize: 1000, current: 1 });
        setState({
            data: [...data],
            totalCount: data.totalCount || 0
        })
        setLoading(false);

    }

    const onChange = (current, pageSize) => {
        setParams({
            ...params,
            current,
            pageSize
        })
    }
    useEffect(() => {
        getList()
    }, [params])

    /**  @description 打开编辑窗口 */
    const open = (type = "add", id, parentId) => {
        console.log("打开")
        cateFormRef.current.open(type, id, parentId);
    }

    /**@description 删除按钮操作 */
    const handleDelete = async (record) => {
        try {
            await deleteConfirm();
            const { code, msg } = await cateApi.deleteCate(record.id)
        } catch (error) {

        } finally {
            getList()
        }

    }


    const [selectedKeys, setSelectedKeys] = useState([])
    const onSelectChange = (newSelectedRowKeys, selectedRows) => {
        setSelectedKeys(newSelectedRowKeys);
    }
    const actionPanel = getActoinPanel({
        editFormClick: open
    })
    const tableProps = {
        tablePanelConfig: actionPanel,
        loading,
        columns: getColumns({ open, handleDelete }),
        dataSource: state.data[0].children,
        pagination: {
            total: state.totalCount,
            pageSize: params.pageSize,
            onChange: onChange,
            current: params.current

        },
        rowSelection: {
            type: "checkbox",
            selectedRowKeys: selectedKeys,
            onChange: onSelectChange,
        },
        rowKey: 'id',
        selectedRowKeys: selectedKeys,
        onRow: (record, index) => {
            return {
                onDoubleClick: () => {
                    console.log("双击", record);
                }
            }
        },
    }


    return (
        <XxpageContainer>
            <CateForm treeData={state.data} onRef={cateFormRef} getList={getList} />
            <TableSearchPanel
                fields={searchFields}
                onSearch={onClickSearch}

            />
            <XxTable  {...tableProps} />

        </XxpageContainer>
    )
}

export default CategoryListPages;